package com.banciyuan.bcywebview.base.factory;

import android.text.TextUtils;

import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import de.greenrobot.daoexample.model.Detail;
import de.greenrobot.daoexample.model.SearchContent;
import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.Timeline;
import de.greenrobot.daoexample.model.TimelineCircle;
import de.greenrobot.daoexample.model.VideoInfo;

/**
 * Created by hfxt2008 on 14-8-22.
 * 生成Timeline类的工厂类，用于解析json，然后生成一个list<Timeline>
 * 也用于其他类型往timeline的变换，所有进入detail的数据最后都会被包装成timeline类
 */
public class TimelineFactory {

    public final static int TIMELINE_TYPE_OTHER = -300;
    public final static int TIMELINE_TYPE_COSER = 300;
    public final static int TIMELINE_TYPE_ILLUST = 301;
    public final static int TIMELINE_TYPE_DAILY = 302;
    public final static int TIMELINE_TYPE_GROUP = 303;
    public final static int TIMELINE_TYPE_ASK = 305;
    public final static int TIMELINE_TYPE_WRITER = 306;
    ///拍立得
    public final static int TIMELINE_TYPE_POLAROID = 307;
    //次元空间 关于他card
    public static final int TIMELINE_TYPE_FOCUSEDGROUP = 308;
    public static final int TIMELINE_TYPE_BLANK = 309;
    //时间轴话题类型
    public static final int TIMELINE_TYPE_GROUP_TEAM = 310;

    //时间轴次元聚焦类型
    public static final int TIMELINE_TYPE_SPOT_TAG = 311;
    public static final int TIMELINE_TYPE_SPOT_WORK = 312;
    public static final int TIMELINE_TYPE_SPOT_TEXT = 313;

    //时间轴圈子类型
    public static final int TIMELINE_TYPE_CIRCLE = 314;

    //空卡片类型
    public static final int TIMELINE_TYPE_EMPTY = 315;

    //制品卡片类型
    public static final int TIMELINE_TYPE_GOODS = 320;

    //lastview
    public static final int TIMELINE_TYPE_LAST_VIEW = 330;

    //专题
    public final static int TIMELINE_TYPE_TOPIC = 340;

    public final static int TIMELINE_TYPE_BANNER = 370;

    public final static int TIMELINE_TYPE_LOCAL = 350;

    public final static int TIMELINE_TYPE_MORE_BAN = 360;

    public final static int TIMELINE_TYPE_LOCAL_CIRCLE = 370;




    public static LinkedList<Timeline> createTimeline(JSONArray json) throws JSONException {
        LinkedList<Timeline> mData = new LinkedList<Timeline>();
        for (int i = 0; i < json.length(); i++) {
            JSONObject detail = new JSONObject();
            Timeline data = new Timeline();
            JSONObject information = new JSONObject();
            try {
                detail = (JSONObject) json.get(i);
                information = detail.getJSONObject("detail");
                if (information.has("type")) {
                    data.setType(information.getString("type"));
                }
                if (!StringUtil.notNullEqual(data.getType(), "artwork")) {
                    try {
                        data.setWork(information.getString("work"));
                    } catch (JSONException e) {
                        data.setWork("");
                    }
                    try {
                        data.setCharacter(information.getString("character"));
                    } catch (JSONException e) {
                        data.setCharacter("");
                    }
                }
            } catch (JSONException e) {
                continue;
            }
            data.setUid(StringUtil.getFromJson(detail, "uid"));
            data.setTl_id(StringUtil.getFromJson(detail, "tl_id"));
            data.setPushtime(StringUtil.getFromJson(detail, "ctime"));
            data.setOuid(StringUtil.getFromJson(detail, "ouid"));
            data.setOtype(StringUtil.getFromJson(detail, "otype"));
            data.setOtype_data(StringUtil.getFromJson(detail, "otype_data"));
            data.setHeadAddress(StringUtil.getFromJson(detail, "avatar"));
            data.setUName(StringUtil.getFromJson(detail, "uname"));
            data.setOUName(StringUtil.getFromJson(detail, "ouname"));
            data.setOavatar(StringUtil.getFromJson(detail, "oavatar"));
            data.setTl_type(StringUtil.getFromJson(detail, "tl_type"));
            data.setRealName(StringUtil.getFromJson(detail, "real_name"));
            data.setTag_id(StringUtil.getFromJson(detail, "tag_id"));
            data.setTag_name(StringUtil.getFromJson(detail, "tag_name"));
            data.setTag_cover(StringUtil.getFromJson(detail, "tag_cover"));
            data.setWork_cover(StringUtil.getFromJson(detail, "work_cover"));
            data.setEvent_id(StringUtil.getFromJson(detail, "event_id"));
            data.setFrom_uname(StringUtil.getFromJson(detail, "from_uname"));
            data.setFrom_uid(StringUtil.getFromJson(detail, "from_uid"));
            data.setPays(StringUtil.getFromJson(detail, "pays"));
            data.setWf_status(StringUtil.getFromJson(detail, "wf_status"));
            data.setTf_status(StringUtil.getFromJson(detail, "tf_status"));
            data.setDetailWid(StringUtil.getFromJson(detail, "wid"));
            if (TextUtils.isEmpty(data.getUName())) data.setUName(data.getOUName());
            if (TextUtils.isEmpty(data.getUid())) data.setUid(data.getOuid());
            if (TextUtils.isEmpty(data.getPushtime()))
                data.setPushtime(StringUtil.getFromJson(information, "ctime"));
            data.setIntro(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "plain")));
            Detail infomationDetail;
            switch (whichType(data)) {
                case TIMELINE_TYPE_GROUP:
                    getTimelineGroupTagDetial(data, information);
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setGid(StringUtil.getFromJson(information, "gid"));
                    data.setGname(StringUtil.getFromJson(information, "group", "name"));
                    data.setPost_id(StringUtil.getFromJson(information, "post_id"));
                    data.setPic_num(StringUtil.getFromJson(information, "pic_num"));
                    data.setMembership(StringUtil.getFromJson(information, "group", "member_num"));
                    data.setPost_num(StringUtil.getFromJson(information, "group", "post_num"));
                    data.setWid(StringUtil.getFromJson(detail, "wid"));
                    if (TextUtils.isEmpty(data.getWid())) {
                        data.setWid(StringUtil.getFromJson(information, "group", "wid"));
                    }
                    data.setWork(StringUtil.getFromJson(information, "group", "work"));
                    data.setTag_name(StringUtil.getFromJson(detail, "tag_name"));
                    try {
                        JSONArray imgArray = information.getJSONArray("multi");
                        StringBuilder img_src = new StringBuilder("");
                        for (int j = 0; j < imgArray.length(); j++) {
                            JSONObject imgJson = imgArray.getJSONObject(j);
                            img_src.append(imgJson.get("path"));
                            img_src.append(" ");
                        }
                        data.setContentAddress(img_src.toString());
                    } catch (JSONException e) {
                        data.setContentAddress("");
                    }
                    break;
                case TIMELINE_TYPE_DAILY:
                    getTimelineTagDetial(data, information);
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setContentAddress(StringUtil.getFromJson(information, "cover"));
                    data.setSummary(StringUtil.getFromJson(information, "summary"));
                    data.setUd_id(StringUtil.getFromJson(information, "ud_id"));
                    data.setWid(StringUtil.getFromJson(information, "wid"));
                    data.setPic_num(StringUtil.getFromJson(information, "pic_num"));
                    if (information.has("video_info")) {
                        JSONObject videoJson = information.getJSONObject("video_info");
                        VideoInfo videoInfo = new VideoInfo();
                        videoInfo.setDuration(StringUtil.getFromJson(videoJson, "duration"));
                        data.setVideo_info(videoInfo);
                    }
                    if (StringUtil.notNullEqual(data.getType(), "daily")) {
                        try {
                            JSONArray imgArray = information.getJSONArray("multi");
                            StringBuilder img_src = new StringBuilder("");
                            for (int j = 0; j < imgArray.length(); j++) {
                                JSONObject imgJson = imgArray.getJSONObject(j);
                                img_src.append(imgJson.get("path"));
                                img_src.append(" ");
                            }
                            data.setContentAddress(img_src.toString());
                        } catch (Exception e) {
                            data.setContentAddress("");
                        }
                    }
                    break;
                case TIMELINE_TYPE_ILLUST:
                case TIMELINE_TYPE_COSER:
                default:
                    getTimelineTagDetial(data, information);
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setRp_id(StringUtil.getFromJson(information, "rp_id"));
                    data.setCp_id(StringUtil.getFromJson(information, "cp_id"));
                    data.setDp_id(StringUtil.getFromJson(information, "dp_id"));
                    data.setContentAddress(StringUtil.getFromJson(information, "img_src"));
                    data.setWid(StringUtil.getFromJson(information, "wid"));
                    break;
                case TIMELINE_TYPE_ASK:
                    infomationDetail = new Detail();
                    data.setRp_id(StringUtil.getFromJson(information, "ua_id"));
                    data.setCp_id(StringUtil.getFromJson(information, "cuname"));
                    data.setDp_id(StringUtil.getFromJson(information, "anonymous"));
                    data.setContentAddress(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "answer", "content")));
                    data.setIntro(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "ask")));
                    infomationDetail.setUa_id(StringUtil.getFromJson(information, "ua_id"));
                    infomationDetail.setCuname(StringUtil.getFromJson(information, "cuname"));
                    infomationDetail.setAnonymous(StringUtil.getFromJson(information, "anonymous"));
                    infomationDetail.setContentAddress(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "answer", "content")));
                    infomationDetail.setAsk(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "ask")));
                    infomationDetail.setCavatar(StringUtil.getFromJson(information, "cavatar"));
                    infomationDetail.setCuid(StringUtil.getFromJson(information, "cuid"));
                    List<Detail> details = new ArrayList<Detail>();
                    data.setDetails(details);
                    data.setDetail(infomationDetail);
                    break;
                case TIMELINE_TYPE_WRITER:
                    getTimelineTagDetial(data, information);
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setRp_id(StringUtil.getFromJson(information, "rp_id"));
                    data.setWp_id(StringUtil.getFromJson(information, "wp_id"));
                    data.setContentAddress(StringUtil.getFromJson(information, "cover"));
                    data.setCommentNum(StringUtil.getFromJson(information, "reply_count"));
                    String postTags = StringUtil.getFromJson(information, "post_tags");
                    data.setWord_count(StringUtil.getFromJson(information, "word_count"));
                    data.setWid(StringUtil.getFromJson(information, "wid"));
                    data.setPost_in_set(StringUtil.getFromJson(information, "post_in_set"));
                    data.setSummary(StringUtil.getFromJson(information, "summary"));
                    List<String> tags = new ArrayList<String>();
                    try {
                        JSONArray array = new JSONArray(postTags);
                        JSONObject obj;
                        String tagName;
                        for (int j = 0; j < array.length(); j++) {
                            obj = new JSONObject(array.get(j).toString());
                            tagName = obj.get("tag_name").toString();
                            tags.add(tagName);
                        }
                        data.setPost_tags(tags);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case TIMELINE_TYPE_GROUP_TEAM:
                    getTimelineTagDetial(data, information);
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setGid(StringUtil.getFromJson(information, "gid"));
                    data.setContentAddress(StringUtil.getFromJson(information, "img_src"));
                    data.setIntro(StringUtil.getFromJson(information, "intro"));
                    data.setWid(StringUtil.getFromJson(information, "wid"));
                    data.setWork(StringUtil.getFromJson(information, "work"));
                    data.setGroupTitle(StringUtil.getFromJson(information, "name"));
                    data.setHaveRecommend(StringUtil.getFromJson(information, "have_tuijian"));
                    data.setPost_num(StringUtil.getFromJson(information, "post_num"));
                    break;
                case TIMELINE_TYPE_SPOT_TAG:
                case TIMELINE_TYPE_SPOT_WORK:
                case TIMELINE_TYPE_SPOT_TEXT:
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    data.setEs_id(StringUtil.getFromJson(information, "es_id"));
                    data.getDetail().setBanner(StringUtil.getFromJson(information, "banner"));
                    data.getDetail().setTitle(StringUtil.getFromJson(information, "title"));
                    data.getDetail().setPlain(StringUtil.getFromJson(information, "plain"));
                    data.getDetail().setCode(StringUtil.getFromJson(information, "code"));
                    break;
                case TIMELINE_TYPE_CIRCLE:
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    List<TimelineCircle> timelineCircles = null;
                    try {
                        timelineCircles = new Gson().fromJson(information.getJSONArray("works").toString(), new TypeToken<List<TimelineCircle>>() {
                        }.getType());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    data.setTimelineCircles(timelineCircles);
                    break;
                case TIMELINE_TYPE_EMPTY:
                    infomationDetail = new Detail();
                    data.setDetail(infomationDetail);
                    break;
                case TIMELINE_TYPE_GOODS:
                    infomationDetail = new Detail();
                    getTimelineTagDetial(data, information);
                    data.setDetail(infomationDetail);
                    data.setIntro(StringUtil.getFromJson(information, "intro"));
                    data.setWid(StringUtil.getFromJson(information, "wid"));
                    data.setWork(StringUtil.getFromJson(information, "work"));
                    data.setZg_id(StringUtil.getFromJson(information, "zg_id"));
                    data.setPrice(StringUtil.getFromJson(information, "basic_price"));
                    data.setContentAddress(StringUtil.getFromJson(information, "cover"));
                    break;
                case TIMELINE_TYPE_BANNER:
                    data.setIntro(StringUtil.getFromJson(information, "intro"));
                    data.setCode(StringUtil.getFromJson(information, "code"));
                    data.setContentAddress(StringUtil.getFromJson(information, "cover"));
                    break;
                case TIMELINE_TYPE_TOPIC:
                    try {
                        List<String> labels = new ArrayList<>();
                        if (information.has("labels")) {
                            JSONArray array = information.getJSONArray("labels");
                            for (int j = 0; j < array.length(); j++) {
                                labels.add(array.getString(j));
                            }
                            data.setLabels(labels);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    data.setIntro(StringUtil.getFromJson(information, "intro"));
                    data.setTitle(StringUtil.getFromJson(information, "title"));
                    data.setSpecial_id(StringUtil.getFromJson(information, "special_id"));
                    data.setContentAddress(StringUtil.getFromJson(information, "cover"));
                    break;
            }

            data.setTitle(StringUtil.htmlDecoder(StringUtil.getFromJson(information, "title")));
            data.setOctime(StringUtil.getFromJson(information, "ctime"));
            data.setCommentNum(StringUtil.getFromJson(information, "reply_count"));
            data.setLikeNum(StringUtil.getFromJson(information, "ding_num"));
            data.setVideoNum(StringUtil.getFromJson(information, "video_num"));
            data.setHaveDing(StringUtil.getFromJson(information, "have_ding"));
            data.setHaveRecommend(StringUtil.getFromJson(information, "have_tuijian"));
            data.setPicNum(StringUtil.getFromJson(information, "pic_num"));
            data.setFollowState(StringUtil.getFromJson(information, "followstate"));
            data.setNeedFans(StringUtil.getFromJson(information, "view_need_fans"));
            data.setNeedLogin(StringUtil.getFromJson(information, "view_need_login"));
            data.setHideAdmin(StringUtil.getFromJson(information, "hide_admin"));
            if (data != null && !data.getHideAdmin().equals("true")) mData.add(data);
            if (data.getTags() != null && !TextUtils.isEmpty(data.getWork())) {
                TagDetail tagDetail = new TagDetail();
                tagDetail.setWork(data.getWork());
                tagDetail.setTag_name(data.getWork());
                tagDetail.setWid(data.getWid());
                data.getTags().add(0, tagDetail);
            }
        }
        return mData;
    }

    private static void getTimelineGroupTagDetial(Timeline data, JSONObject information) {
        try {
            Gson gson = new Gson();
            List<TagDetail> workTags = gson.fromJson(StringUtil.getFromJson(information, "group", "post_tags"), new TypeToken<List<TagDetail>>() {
            }.getType());
            data.setTags(workTags);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getTimelineTagDetial(Timeline data, JSONObject information) {
        try {
            Gson gson = new Gson();
            List<TagDetail> workTags = gson.fromJson(StringUtil.getFromJson(information, "post_tags"), new TypeToken<List<TagDetail>>() {
            }.getType());
            data.setTags(workTags);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void popNewCircleIllegalType(List<TagDetail> tagDetails) {
        if (null == tagDetails)
            return;
        List<TagDetail> illegalData = new ArrayList<>();
        for (int i = 0; i < tagDetails.size(); i++) {
            String type = tagDetails.get(i).getType();
            if (!"work".equals(type) && !"tag".equals(type) && !"event".equals(type)) {
                illegalData.add(tagDetails.get(i));
            }
        }
        tagDetails.removeAll(illegalData);
    }

    //用来将当前list中非法的type踢出去
    public static void popIllegalItem(List<Timeline> mData) {
        LinkedList<Timeline> delList = new LinkedList<Timeline>();
        for (Timeline timeline : mData) {
            if (illegalType(timeline))
                delList.add(timeline);
        }
        mData.removeAll(delList);
    }

    public static void popCircleIllegalItem(List<Timeline> mData) {
        LinkedList<Timeline> delList = new LinkedList<Timeline>();
        for (Timeline timeline : mData) {
            if (illegalCircleType(timeline))
                delList.add(timeline);
        }
        mData.removeAll(delList);
    }

    public static void popCircleIllegalGridItem(List<Timeline> mData) {
        List<Timeline> delList = new ArrayList<>();
        for (Timeline timeline : mData) {
            if (illegalCircleType(timeline.getOtype_data(), timeline.getOtype()))
                delList.add(timeline);
        }
        mData.removeAll(delList);
    }

    //用来判断当前timeline中的card是否合法，具体细节请向服务器端人员咨询
    public static Boolean illegalType(Timeline mItem) {
        switch (whichType(mItem)) {
            case TIMELINE_TYPE_DAILY:
            case TIMELINE_TYPE_GROUP:
            case TIMELINE_TYPE_ILLUST:
            case TIMELINE_TYPE_COSER:
            case TIMELINE_TYPE_ASK:
            case TIMELINE_TYPE_WRITER:
            case TIMELINE_TYPE_GROUP_TEAM:
            case TIMELINE_TYPE_GOODS:
                return false;
            default:
                return true;
        }
    }

    public static Boolean illegalCircleType(Timeline mItem) {
        switch (whichType(mItem)) {
            case TIMELINE_TYPE_ILLUST:
            case TIMELINE_TYPE_COSER:
            case TIMELINE_TYPE_WRITER:
            case TIMELINE_TYPE_DAILY:
            case TIMELINE_TYPE_GOODS:
            case TIMELINE_TYPE_GROUP_TEAM:
            case TIMELINE_TYPE_EMPTY:
            case TIMELINE_TYPE_LOCAL:
                return false;
            default:
                return true;
        }
    }

    public static Boolean illegalCircleType(String otypedata, String otype) {
        switch (whichType(otypedata, otype)) {
            case TIMELINE_TYPE_ILLUST:
            case TIMELINE_TYPE_COSER:
            case TIMELINE_TYPE_WRITER:
            case TIMELINE_TYPE_DAILY:
            case TIMELINE_TYPE_GOODS:
            case TIMELINE_TYPE_GROUP_TEAM:
                return false;
            default:
                return true;
        }
    }

    public static Boolean illegalFeedType(String type) {
        switch (type) {
            case "cos":
            case "illust":
            case "novel":
            case "daily":
                return false;
            default:
                return true;
        }
    }

    //用来判断当前timeline中的card具体是哪一种type
    public static int whichType(Timeline mItem) {
        String otype = mItem.getOtype();
        String otype_data = mItem.getOtype_data();
        return whichType(otype_data, otype);
    }

    //判断当前timeline中的card具体是哪一种type,不依赖与timeline 对象
    public static int whichType(String otypeData, String otype) {
        if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_DAILY_DATA)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_DAILY_NAME))
                return TIMELINE_TYPE_DAILY;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_GROUP_DATA)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_GROUP_NAME))
                return TIMELINE_TYPE_GROUP;
            else if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_TIMELINE_ILL_NAME))
                return TIMELINE_TYPE_ILLUST;
            else if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_TIMELINE_COS_NAME))
                return TIMELINE_TYPE_COSER;
            else if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_WRITER_DATA))
                return TIMELINE_TYPE_WRITER;
            else if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_SPECIAL))
                return TIMELINE_TYPE_TOPIC;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_GROUP_GROUPDATA)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_GROUP_NAME))
                return TIMELINE_TYPE_GROUP_TEAM;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_ASK_DATA)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_ASK_NAME))
                return TIMELINE_TYPE_ASK;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_POLAROID_DATA)) {
            return TIMELINE_TYPE_POLAROID;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_PERSON_ABOUT)) {
            return TIMELINE_TYPE_FOCUSEDGROUP;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_BLANK)) {
            return TIMELINE_TYPE_BLANK;
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_TAG)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_SPOT)) {
                return TIMELINE_TYPE_SPOT_TAG;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_WORK)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_SPOT)) {
                return TIMELINE_TYPE_SPOT_WORK;
            } else if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_CIRCLE)) {
                return TIMELINE_TYPE_CIRCLE;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_TEXT)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_SPOT)) {
                return TIMELINE_TYPE_SPOT_TEXT;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_EMPTY)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_EMPTY)) {
                return TIMELINE_TYPE_EMPTY;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_GOODS)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_ZHIPIN)) {
                return TIMELINE_TYPE_GOODS;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_LAST_VIEW)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_LAST_VIEW)) {
                return TIMELINE_TYPE_LAST_VIEW;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_LOCAL)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_LOCAL)) {
                return TIMELINE_TYPE_LOCAL;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_MORE_BAN)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_MORE_BAN)) {
                return TIMELINE_TYPE_MORE_BAN;
            }
        } else if (StringUtil.notNullEqual(otypeData, MyConstants.OTYPE_BANNER)) {
            if (StringUtil.notNullEqual(otype, MyConstants.OTYPE_BANNER)) {
                return TIMELINE_TYPE_BANNER;
            }
        }
        return TIMELINE_TYPE_OTHER;

    }

    public static int whichType(SearchContent searchContent) {
        Timeline tempLine = new Timeline();
        tempLine.setOtype(searchContent.getOtype());
        tempLine.setOtype_data(searchContent.getOtype_data());
        return whichType(tempLine);
    }

    public static LinkedList<Timeline> processAskTimeline(LinkedList<Timeline> mList) {
        int count = 1;
        mList.add(new Timeline());
        List<Detail> tempDetails = new ArrayList<>();
        LinkedList<Timeline> newList = new LinkedList<>();
        for (int i = 0; i < mList.size() - 1; i++) {
            Timeline nowLine = mList.get(i);
            Timeline nextLine = mList.get(i + 1);
            if (whichType(nowLine) == TIMELINE_TYPE_ASK && whichType(nextLine) == TIMELINE_TYPE_ASK &&
                    nowLine.getOuid().equals(nextLine.getOuid())) {
                count++;
            } else {
                if (count == 1) {
                    newList.add(mList.get(i));
                } else if (count == 2) {
                    newList.add(mList.get(i - 1));
                    newList.add(mList.get(i));
                } else if (count >= 3) {
                    for (int j = count; j > 0; j--) {
                        int index = i + 1 - j;
                        tempDetails.add(mList.get(index).getDetail());
                    }
                    mList.get(i + 1 - count).getDetails().addAll(tempDetails);
                    mList.get(i + 1 - count).setTl_id(mList.get(i).getTl_id());
                    newList.add(mList.get(i + 1 - count));
                    tempDetails.clear();
                }
                count = 1;
            }
        }

        return newList;
    }

    public static void removeDuplicate(LinkedList<Timeline> mList, List<String> timeLineKeys) {
        LinkedList<Timeline> newList = new LinkedList<>();
        for (int i = 0; i < mList.size(); i++) {
            Timeline timeline = mList.get(i);
            String key = "";
            switch (TimelineFactory.whichType(timeline)) {
                case TimelineFactory.TIMELINE_TYPE_COSER:
                    key = "coser_" + timeline.getCp_id() + "_" + timeline.getRp_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_ILLUST:
                    key = "drawer_" + timeline.getDp_id() + "_" + timeline.getRp_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_WRITER:
                    key = "writer_" + timeline.getWp_id() + "_" + timeline.getRp_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_ASK:
                    key = "ask_" + timeline.getRp_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_DAILY:
                    key = "daily_" + timeline.getUd_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_GROUP:
                    key = "group_" + timeline.getPost_id() + "_" + timeline.getGid();
                    break;
                case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                    key = "groupteam_" + timeline.getGid();
                    break;
                case TimelineFactory.TIMELINE_TYPE_SPOT_TAG:
                case TimelineFactory.TIMELINE_TYPE_SPOT_TEXT:
                case TimelineFactory.TIMELINE_TYPE_SPOT_WORK:
                    key = "spot_" + timeline.getEs_id() + timeline.getPushtime();
                    break;
                case TimelineFactory.TIMELINE_TYPE_GOODS:
                    key = "goods_" + timeline.getZg_id();
                    break;
                case TimelineFactory.TIMELINE_TYPE_EMPTY:
                    key = "empty";
                    break;
                case TimelineFactory.TIMELINE_TYPE_BANNER:
                    key = "banner_" + System.currentTimeMillis();
                    break;
                case TimelineFactory.TIMELINE_TYPE_TOPIC:
                    key = "special_" + timeline.getSpecial_id();
                    break;
            }
            if (StringUtil.notNullEqual(timeline.getTl_type(), "toppay")) {
                key += "toppay";
            }
            boolean contains = false;
            for (int j = 0; j < timeLineKeys.size(); j++) {
                if (timeLineKeys.get(j).equals(key)) {
                    newList.add(timeline);
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                //数据不存在，保留,存key,添加
                timeLineKeys.add(key);
            }
        }
        mList.removeAll(newList);
    }

    public static String getTimeLineTypeName(Timeline timeline) {
        int type = TimelineFactory.whichType(timeline);
        return getTimeLineTypeName(type);
    }

    public static String getTimeLineTypeName(int type) {
        String type_name = "";
        switch (type) {
            case TimelineFactory.TIMELINE_TYPE_COSER:
                type_name = "cos";
                break;
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                type_name = "illust";
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:
                type_name = "novel";
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                type_name = "daily";
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                type_name = "group_post";
                break;
            case TimelineFactory.TIMELINE_TYPE_ASK:
                type_name = "ask";
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                type_name = "group";
                break;
            case TimelineFactory.TIMELINE_TYPE_GOODS:
                type_name = "zhipin_goods";
                break;
            default:
                break;
        }
        return type_name;
    }

    public static String getTimeLineid(Timeline timeline) {
        int type = TimelineFactory.whichType(timeline);
        String type_name = "";
        switch (type) {
            case TimelineFactory.TIMELINE_TYPE_COSER:
                type_name = timeline.getRp_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                type_name = timeline.getRp_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:
                type_name = timeline.getRp_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                type_name = timeline.getUd_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                type_name = timeline.getPost_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_ASK:
                type_name = timeline.getDetail().getUa_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                type_name = timeline.getGid();
                break;
            case TimelineFactory.TIMELINE_TYPE_SPOT_TAG:
            case TimelineFactory.TIMELINE_TYPE_SPOT_WORK:
            case TimelineFactory.TIMELINE_TYPE_SPOT_TEXT:
                type_name = timeline.getEs_id();
                break;
            case TimelineFactory.TIMELINE_TYPE_GOODS:
                type_name = timeline.getZg_id();
                break;
            default:
                break;
        }
        return type_name;
    }

    //timeline接口 用来将当前list中非法的type踢出去
    public static void popIllegalItemNew(LinkedList<Timeline> mData, int count) {
        LinkedList<Timeline> delList = new LinkedList<>();
        for (Timeline timeline : mData) {
            if (illegalTypeNew(timeline, count))
                delList.add(timeline);
        }
        mData.removeAll(delList);
    }

    //timeline接口 用来将当前list中非法的type踢出去
    public static void popIllegalItemDiscover(LinkedList<Timeline> mData) {
        LinkedList<Timeline> delList = new LinkedList<>();
        for (Timeline timeline : mData) {
            if (illegalTypeDiscover(timeline))
                delList.add(timeline);
        }
        mData.removeAll(delList);
    }

    public static boolean illegalTypeDiscover(Timeline mItem) {
        switch (whichType(mItem)) {
            case TIMELINE_TYPE_GROUP:
                return !("work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "discover".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_ILLUST:
            case TIMELINE_TYPE_COSER:
            case TIMELINE_TYPE_WRITER:
                return !("work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "discover".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_DAILY:
                return !("tag".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "discover".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_GROUP_TEAM:
                return !("work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "discover".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_GOODS:
                return !("tag".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "discover".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_BANNER:
                return !"ext".equals(mItem.getTl_type());
            case TIMELINE_TYPE_TOPIC:
                return !"ext".equals(mItem.getTl_type());
            default:
                return true;
        }
    }

    //timeline接口的排除不合法数据
    public static boolean illegalTypeNew(Timeline mItem, int count) {
        switch (whichType(mItem)) {
            case TIMELINE_TYPE_GROUP:
                return !("user".equals(mItem.getTl_type())
                        || "group".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "site_work".equals(mItem.getTl_type())
                        || "site_tag".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_ILLUST:
            case TIMELINE_TYPE_COSER:
            case TIMELINE_TYPE_WRITER:
                return !("work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "user".equals(mItem.getTl_type())
                        || "toppay".equals(mItem.getTl_type())
                        || "site_work".equals(mItem.getTl_type())
                        || "site_tag".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_DAILY:
                return !("user".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "toppay".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "site_work".equals(mItem.getTl_type())
                        || "site_tag".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_ASK:
                return !"user".equals(mItem.getTl_type());
            case TIMELINE_TYPE_GROUP_TEAM:
                return !("user".equals(mItem.getTl_type())
                        || "group".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "site_work".equals(mItem.getTl_type())
                        || "site_tag".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_SPOT_TAG:
            case TIMELINE_TYPE_SPOT_WORK:
            case TIMELINE_TYPE_SPOT_TEXT:
                return !("ext".equals(mItem.getTl_type()));
            case TIMELINE_TYPE_CIRCLE:
                return !"ext".equals(mItem.getTl_type());
            case TIMELINE_TYPE_EMPTY:
                if (count > 0) {
                    return true;
                } else {
                    return !"empty".equals(mItem.getTl_type());
                }
            case TIMELINE_TYPE_GOODS:
                return !("user".equals(mItem.getTl_type())
                        || "tag".equals(mItem.getTl_type())
                        || "work".equals(mItem.getTl_type())
                        || "site_work".equals(mItem.getTl_type())
                        || "site_tag".equals(mItem.getTl_type()));
            default:
                return true;
        }
    }


}